//
//  MasterScrollView.m
//  Documents
//
//  Created by Alex Di Mango 
//  Copyright (c) 2012 Whardoo.com -  All rights reserved.
//

#import "DocumentsViewController.h"
#import "MasterScrollView.h"
#import "FolderThumbView.h"
#import "DocumentThumbView.h"
#import "DocumentsConstants.h"

@implementation MasterScrollView

@synthesize currentDocument,firstDocument,secondDocument,selectedReaderDocument,comparing;
@synthesize readerViewController;

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
    }
    return self;
}

#pragma mark - DroppableTableViewDelegate

-(void)dragAndDropThumbViewController:(UIView *)ddtvc droppedGesture:(UIGestureRecognizer *)gesture
{
    UIView *viewHit = [self hitTest:[gesture locationInView:self] withEvent:nil];
    MyDocument *myDoc = [MyDocument getDocumentWithId:[NSNumber numberWithInt:ddtvc.tag]];
    DocumentsViewController *home = (DocumentsViewController *)self.superview.nextResponder; 

    
    if ([viewHit isKindOfClass:[FolderThumbView class]])
    {        NSLog(@"Item dropped OK");
        MyDocumentsFolder *myFolder = [MyDocumentsFolder getFolderWithId:[NSNumber numberWithInt:viewHit.tag]];   
        [myDoc setMyFolder:myFolder];
        [home setSelectedFolder:myFolder];
        [home setSelectedBorder:nil];
        [home setSelectedDocument:nil];
        [home setDidDragged:YES];
        [[NSNotificationCenter defaultCenter] postNotificationName:REFRESH_FOLDERS_AND_DOCS object:self];

    }    
    else{
        [home setSelectedFolder:myDoc.myFolder];
        [[NSNotificationCenter defaultCenter] postNotificationName:REFRESH_FOLDERS_AND_DOCS object:self];
        NSLog(@"Item dropped on a non droppable area");
}
}

- (BOOL)touchesShouldCancelInContentView:(UIView *)view
{
    BOOL shouldCancel = NO;
    if([view isKindOfClass:[DocumentThumbView class]])
        shouldCancel = YES;
        return shouldCancel;
}

#pragma mark - Reader View Controller

-(void)showDocument: (NSNumber *)fileId{
    
    MyDocument *myDoc = [MyDocument getDocumentWithId:fileId];
    [myDoc setUpdateDate:[NSDate date]];
    [(AppDelegate *)[[UIApplication sharedApplication] delegate] saveContext];
    ComparatorManager *cm = [ComparatorManager sharedInstance];
    
    if([cm isComparing])
    {
        SplittedDocumentViewController *compareView = [cm compareWithDocument:myDoc];
        DocumentsViewController *root = (DocumentsViewController *)self.superview.nextResponder;
        root.parentViewController.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
        root.parentViewController.modalPresentationStyle = UIModalPresentationFullScreen;
        [root.parentViewController presentModalViewController:(UIViewController *)compareView animated:YES];  
        
    }
    else
    {
        self.currentDocument = myDoc;
        [cm setFirstDocument:self.currentDocument];
        
        self.selectedReaderDocument = [ReaderDocument withDocumentFilePath:currentDocument.filePath localFileName:[currentDocument.documentId stringValue] fileType:currentDocument.getFileType password:nil document:currentDocument];
        if (selectedReaderDocument)
        {
            [self documentLoaded];
        }
    }
}

-(void)documentLoaded
{
    if (!selectedReaderDocument)
    {        
        selectedReaderDocument = [ReaderDocument withDocumentFilePath:currentDocument.filePath localFileName:[currentDocument.documentId stringValue] fileType:currentDocument.getFileType password:nil document:currentDocument];
    }
    
    self.firstDocument = currentDocument;
    
    ReaderViewController *_readerViewController = [[ReaderViewController alloc] initWithReaderDocument:selectedReaderDocument];
    self.readerViewController = _readerViewController;
    [_readerViewController release];
    self.readerViewController.showInMyDocuments = YES;
    self.readerViewController.delegate = self; // Set the ReaderViewController delegate to self
    
    // present in a modal view controller
    self.readerViewController.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
    self.readerViewController.modalPresentationStyle = UIModalPresentationFullScreen;
    DocumentsViewController *root = (DocumentsViewController *)self.superview.nextResponder;
    [root.parentViewController presentModalViewController:self.readerViewController animated:YES];    
}

-(void)dismissReaderViewController:(ReaderViewController *)viewController
{
    DocumentsViewController *root = (DocumentsViewController *)self.superview.nextResponder;
    [root.parentViewController dismissModalViewControllerAnimated:YES];
    self.selectedReaderDocument = nil;
    self.readerViewController = nil;
}

-(void)dealloc{
    [currentDocument release];
    [firstDocument release];
    [secondDocument release];
    [selectedReaderDocument release];
    [readerViewController release];
    [super dealloc];
}
@end
